﻿<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title>ContextMenu Tree右键菜单</title>
    <meta http-equiv="content-type" content="text/html; charset=UTF-8" /><link href="../demo.css" rel="stylesheet" type="text/css" />

    
    <script src="../../scripts/boot.js" type="text/javascript"></script>
    
    

</head>
<body>
    <h1>ContextMenu Tree右键菜单</h1>      

    <ul id="tree1" class="mini-tree" url="../data/listTree.txt" style="width:200px;padding:5px;" 
        showTreeIcon="true" textField="text" idField="id" parentField="pid" resultAsTree="false"          
        contextMenu="#treeMenu" expandOnLoad="true"
        >
    </ul>
    

<ul id="treeMenu" class="mini-contextmenu"  onbeforeopen="onBeforeOpen">        
    <li iconCls="icon-move" onclick="onMoveNode">移动节点</li>
    <li class="separator"></li>
    <li>
		<span iconCls="icon-add">新增节点</span>
		<ul>
		    <li onclick="onAddBefore">插入节点前</li>                
            <li onclick="onAddAfter">插入节点后</li>	
			<li onclick="onAddNode">插入子节点</li>	             
		</ul>
	</li>
	<li name="edit" iconCls="icon-edit" onclick="onEditNode">编辑节点</li>
	<li name="remove" iconCls="icon-remove" onclick="onRemoveNode">删除节点</li>        
</ul>

        
    <script type="text/javascript">
        function onAddBefore(e) {
            var tree = mini.get("tree1");
            var node = tree.getSelectedNode();

            var newNode = {};
            tree.addNode(newNode, "before", node);
        }
        function onAddAfter(e) {
            var tree = mini.get("tree1");
            var node = tree.getSelectedNode();

            var newNode = {};
            tree.addNode(newNode, "after", node);
        }
        function onAddNode(e) {
            var tree = mini.get("tree1");
            var node = tree.getSelectedNode();

            var newNode = {};
            tree.addNode(newNode, "add", node);
        }
        function onEditNode(e) {
            var tree = mini.get("tree1");
            var node = tree.getSelectedNode();
            
            tree.beginEdit(node);            
        }
        function onRemoveNode(e) {
            var tree = mini.get("tree1");
            var node = tree.getSelectedNode();

            if (node) {
                if (confirm("确定删除选中节点?")) {
                    tree.removeNode(node);
                }
            }
        }
        function onMoveNode(e) {
            var tree = mini.get("tree1");
            var node = tree.getSelectedNode();

            alert("moveNode");
        }
function onBeforeOpen(e) {
    var menu = e.sender;
    var tree = mini.get("tree1");

    var node = tree.getSelectedNode();
    if (!node) {
        e.cancel = true;
        return;
    }
    if (node && node.text == "Base") {
        e.cancel = true;
        //阻止浏览器默认右键菜单
        e.htmlEvent.preventDefault();
        return;
    }

    ////////////////////////////////
    var editItem = mini.getbyName("edit", menu);
    var removeItem = mini.getbyName("remove", menu);
    editItem.show();
    removeItem.enable();

    if (node.id == "forms") {
        editItem.hide();
    }
    if (node.id == "lists") {
        removeItem.disable();
    }
}
    </script>

</body>
</html>